package com.scommunity.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scommunity.domain.Car;
import com.scommunity.mapper.CarMapper;
import com.scommunity.service.ICarService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CarServiceImpl extends ServiceImpl<CarMapper,Car> implements ICarService{
    @Autowired
    CarMapper carMapper;

    @Override
    public PageInfo<Car> all(int currentPage,int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Car> list = carMapper.selectList(null);
        PageInfo<Car> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Car add(Car car) {
        //car.setCarId(2L);
        //car.setCarBrand("法拉利");
        carMapper.insert(car);
        return carMapper.selectOne(new QueryWrapper<Car>().eq("owner_id",car.getOwnerId())
                .eq("car_num",car.getCarNum()).eq("car_brand",car.getCarBrand())
                .eq("car_type",car.getCarType()).eq("car_status",car.getCarStatus()));
    }

    @Override
    public void delete(Car car) {
        carMapper.deleteById(car.getCarId());
    }

    @Override
    public void updateN(Car car) {
        carMapper.updateById(car);
    }

    @Override
    public PageInfo<Car> getPageByCondition(Car car, int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Car> list = carMapper.getByCondition(car);
        PageInfo<Car> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<Car> getByOwnerId(Long ownerId) {
        return carMapper.selectList(new QueryWrapper<Car>().eq("owner_id",ownerId));
    }

    @Override
    public void addTime(Car car) {
        System.out.println(car);
        carMapper.addByCarNum(car);
    }


}
